// 要用来作为dva的数据流的仓库
import delay from '@/utils/delay';

interface StateType {
  name: string;
  num: number;
}

export default {
  // 仓库的数据
  state: {
    name: 'tom',
    num: 10,
  } as StateType,

  // effects是放异步函数的，相当于vuex的actions
  effects: {
    // *queryUser({ payload }, { call, put }) {
    //   const { data } = yield call(queryUser, payload);
    //   yield put({ type: 'queryUserSuccess', payload: data });
    // },

    *asyncIncrement({ n }: any, { call, put }: any) {
      console.log('asyncIncrement', n);
      yield delay(1000);
      yield put({ type: 'increment', n });
    },
  },

  // reducers是放同步函数的，相当于vuex的mutations
  reducers: {
    // queryUserSuccess(state, { payload }) {
    //   return {
    //     ...state,
    //     user: payload,
    //   };
    // },
    changeName(state: StateType, { name }: any) {
      return {
        ...state,
        name,
      };
    },
    increment(state: StateType, { n = 1 }: { n: number }) {
      return {
        ...state,
        num: state.num + n,
      };
    },
  },
};
